# coding = utf-8
# File date: Hi_2022/11/7 22:46
# File_name: recordingPlayback.py
"""鼠标动作工具库,直接实现点击 移动 滚动的操作"""
import time
from tkinter import END
from tkinter.scrolledtext import ScrolledText
import pynput
from pynput.mouse import Button
from GUI_log import log_out

# 日志初始化
g_log = log_out()


class mouse_method:
    """鼠标实例化"""

    def __init__(self):
        # 鼠标实例化
        self.mouse = pynput.mouse.Controller()
        g_log.success("鼠标实例化")

    def mouse_move(self, x, y, sleep_time: float):
        """
        :param x: x坐标,相对屏幕的绝对值
        :param y: Y坐标,相对屏幕的绝对值
        :param sleep_time: 动作间隔时间,会先在执行动作前休眠此事件再执行
        :return: 鼠标在规定时间移动到指定点
        """
        g_log.info(f"执行鼠标到({x}, {y})前先休眠{sleep_time}秒")
        time.sleep(sleep_time)
        self.mouse.position = (x, y)
        g_log.info(f"执行鼠标到({x}, {y})完成")

    @staticmethod
    def write_mouse_move(text: ScrolledText, x, y, sleep_time: float):
        """
        :param text: 需要写入的滚动文本框
        :param x: x坐标,相对屏幕的绝对值
        :param y: Y坐标,相对屏幕的绝对值
        :param sleep_time: 动作间隔时间,会先在执行动作前休眠此事件再执行
        :return: 向滚动文本框最后写入调用mouse_move方法的字符串
        """
        text.insert(END, f"mouse_move({x}, {y}, {sleep_time})\n")
        g_log.info(f"mouse_move({x}, {y}, {sleep_time}) 插入滚动文本框")

    def mouse_press(self, mouse_button: Button, sleep_time: float):
        """
        :param mouse_button: 需要按下的按键字符串需要转换成如Key.left
        :param sleep_time: 动作间隔时间,会先在执行动作前休眠此事件再执行
        :return: 按下指定按键,但不会释放
        """
        g_log.info(f"执行鼠标{mouse_button}按下前先休眠{sleep_time}秒")
        time.sleep(sleep_time)
        self.mouse.press(mouse_button)
        g_log.success(f"执行鼠标{mouse_button}按下完成")

    @staticmethod
    def write_mouse_press(text: ScrolledText, mouse_button: str, sleep_time: float):
        """
        :param text: 需要写入的滚动文本框
        :param mouse_button: 需要按下的按键字符串需要转换成如self.mouse_left
        :param sleep_time: 动作间隔时间,会先在执行动作前休眠此事件再执行
        :return: 向滚动文本框最后写入调用mouse_press方法的的字符串
        """
        text.insert(END, f"mouse_press({mouse_button}, {sleep_time})\n")
        g_log.success(f"mouse_press({mouse_button}, {sleep_time}) 插入滚动文本框")

    def mouse_release(self, mouse_button: Button, sleep_time: float):
        """
        g_log.info(f"执行移动前先休眠{sleep_time}秒")
        :param mouse_button: 需要松开的键位
        :param sleep_time: 动作间隔时间,会先在执行动作前休眠此事件再执行
        :return: 按下指定按键,但不会释放
        """
        g_log.info(f"执行鼠标{mouse_button}释放前先休眠{sleep_time}秒")
        time.sleep(sleep_time)
        self.mouse.release(mouse_button)
        g_log.success(f"执行鼠标{mouse_button}释放完成")

    @staticmethod
    def write_mouse_release(text: ScrolledText, mouse_button: str, sleep_time: float):
        """
        :param text: 需要写入的滚动文本框
        :param mouse_button: 需要松开的按键字符串需要转换成如Key.left
        :param sleep_time: 动作间隔时间,会先在执行动作前休眠此事件再执行
        :return: 向滚动文本框最后写入调用mouse_release方法的的字符串
        """
        text.insert(END, f"mouse_release({mouse_button}, {sleep_time})\n")
        g_log.success(f"mouse_release({mouse_button}, {sleep_time}) 插入滚动文本框")

    def mouse_scroll(self, x, y, sleep_time: float):
        """
        :param x:
        :param y: y为负数向上,正数向下
        :param sleep_time: 动作间隔时间,会先在执行动作前休眠此事件再执行
        :return: 滚动鼠标滚轮
        """
        g_log.info(f"执行鼠标滚轮向{'上' if y > 0 else '下'}滚动前先休眠{sleep_time}秒")
        time.sleep(sleep_time)
        self.mouse.scroll(x, y)
        g_log.success(f"执行鼠标滚轮向{'上' if y > 0 else '下'}滚动完成")

    @staticmethod
    def write_mouse_scroll(text: ScrolledText, dx, dy, sleep_time: float):
        """
        :param text: 需要写入的滚动文本框
        :param dx:
        :param dy: dy为负数向上,正数向下
        :param sleep_time: 动作间隔时间,会先在执行动作前休眠此事件再执行
        :return: 向滚动文本框最后写入调用mouse_scroll方法的的字符串
        """
        text.insert(END, f"mouse_scroll({dx}, {dy}, {sleep_time})\n")
        g_log.success(f"mouse_scroll({dx}, {dy}, {sleep_time}) 插入滚动文本框")
